@file:Suppress("SpellCheckingInspection")

package com.example.piece.ui.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.gyf.immersionbar.ktx.statusBarHeight
import com.example.base.ext.getViewModel
import com.example.base.ui.activity.BaseActivity
import com.example.piece.R
import com.example.piece.databinding.ActivityLayer3Binding
import com.example.piece.vm.Layer3ViewModel
import com.gyf.immersionbar.ktx.immersionBar


/**
 * 三层分布式 Activity
 * @param CVDB 中层布局的 DataBinding 泛型约束
 * @param BVDB 底层布局的 DataBinding 泛型约束
 */
open  class Layer3Activity<CVDB: ViewDataBinding, BVDB: ViewDataBinding> : BaseActivity<ActivityLayer3Binding>() {

    override fun getContentRes(): Int = R.layout.activity_layer_3

    override val isTransparentStatusBar: Boolean = true

    /**  当前界面的 title  **/
    protected open val title: String by lazy {
        getString(R.string.default_title)
    }

    /**  Layer3Activity 中的基础 ViewModel  **/
    @Suppress("MemberVisibilityCanBePrivate")
    // protected val baseViewModel: Layer3ViewModel = lazyViewModel()
    protected val baseViewModel: Layer3ViewModel by lazy {
        getViewModel<Layer3ViewModel>()
    }

    @CallSuper
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mBinding.data = baseViewModel
        mBinding.statusBarHeight = statusBarHeight

        // Set UI title
        baseViewModel.title.value = title

        // Init icons data
        mBinding.leftIcon1Res = leftIcon1Res
        mBinding.leftIcon2Res = leftIcon2Res
        mBinding.rightIcon1Res = rightIcon1Res

        // Set Content UI
        addBottomView()
        addCenterView()

        immersionBar {
            fitsSystemWindows(false)
            statusBarDarkFont(true)
            //  statusBarAlpha(0.1f)
        }
    }

    /**  左边第一个图标的资源 id  **/
    @DrawableRes
    protected open val leftIcon1Res: Int = 0

    /**  左边第二个图标的资源 id  **/
    @DrawableRes
    protected open val leftIcon2Res: Int = 0

    /**  右边第一个图标的资源 id  **/
    @DrawableRes
    protected open val rightIcon1Res: Int = 0

    /**  中层布局的 id  **/
    @LayoutRes
    protected open val centerLayoutRes: Int = 0

    /**  中部 View 的 DataBinding 对象  **/
    @Suppress("MemberVisibilityCanBePrivate")
    protected val mCenterViewBinding: CVDB? by lazy {
        if (centerLayoutRes == 0) null
        else DataBindingUtil.inflate<CVDB>(
            LayoutInflater.from(this),
            centerLayoutRes,
            null,
            false
        )
    }

    /**  向整体布局添加中部布局  **/
    private fun addCenterView() {
        if (mCenterViewBinding == null) return
        val layoutParams = ConstraintLayout.LayoutParams(0, 0)
        layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
        layoutParams.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
        layoutParams.topToBottom = R.id.tvTitle
        if (mBottomViewBinding != null) layoutParams.bottomToTop = R.id.layoutBottom
        else layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
        mCenterViewBinding?.apply {
            root.layoutParams = layoutParams
            root.id = R.id.layoutCenter
        }
        mBinding.rootView.addView(mCenterViewBinding?.root)
    }

    /**  底层布局的 id  **/
    @LayoutRes
    protected open val bottomLayoutRes: Int = 0

    /**  底部 View 的 DataBinding 对象  **/
    @Suppress("MemberVisibilityCanBePrivate")
    protected val mBottomViewBinding: BVDB? by lazy {
        if (bottomLayoutRes == 0) null
        else DataBindingUtil.inflate<BVDB>(
            LayoutInflater.from(this),
            bottomLayoutRes,
            null,
            false
        )
    }

    /**  向整体布局添加底部布局  **/
    private fun addBottomView() {
        if (mBottomViewBinding == null) return
        val layoutParams =
            ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
        layoutParams.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
        mBottomViewBinding?.apply {
            root.layoutParams = layoutParams
            root.id = R.id.layoutBottom
        }
        mBinding.rootView.addView(mBottomViewBinding?.root)
    }

    /**  左边第一个图标的点击事件  **/
    open fun onClickLeftIcon1(view: View) {

    }

    /**  左边第二个图标的点击事件  **/
    open fun onClickLeftIcon2(view: View) {

    }

    /**  右边第一个图标的点击事件  **/
    open fun onClickRightIcon1(view: View) {

    }

}